<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="电话号码" align="center" prop="phonenumber" />
      <el-table-column label="角色" align="center" prop="roleIds">
        <template #default="scope">
          {{ scope.row.roleIds[0] == "2" ? "用户" : "员工" }}</template
        >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="usersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入电话号码" />
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup >
import { listUser, addUser, updateUser, delUser } from "@/api/system/user";

const { proxy } = getCurrentInstance();

const open = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
import { ElMessage, ElMessageBox } from "element-plus";
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: null,
    password: null,
    email: null,
    phone: null,
  },
  rules: {
    userName: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
    password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const userList = ref([]);
//获取用户列表
const getUserList = async () => {
  const { rows } = await listUser({ pageNum: 1, pageSize: 10000 });
  userList.value = rows.filter((v) => v.roleIds[0] === 2);
  console.log(userList.value);
};

// 根据id查询用户
const findUserById = (id) => {
  const current = userList.value.filter((v) => v.userId === id)[0];
  return current.userName;
};

getUserList();

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    userName: null,
    password: null,
    email: null,
    phonenumber: null,
    nickName: "",
    roleIds: [5],
    status: 0,
    vip: false,
    roleIds: [],
  };
  proxy.resetForm("usersRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  console.log(111, row);
  form.value.userId = row.userId;
  form.value.userName = row.userName;
  form.value.password = row.password;
  form.value.email = row.email;
  form.value.phonenumber = row.phonenumber;
  form.value.nickName = row.nickName;
  form.value.roleIds = row.roleIds;
  form.value.status = row.status;
  form.value.vip = row.vip;
  open.value = true;
  title.value = "修改用户";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["usersRef"].validate((valid) => {
    if (valid) {
      if (form.value.userId != null) {
        updateUser(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getUserList();
        });
      } else {
        addUser({ ...form.value, nickName: form.value.userName }).then(
          (response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getUserList();
          }
        );
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm("确定删除吗?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delUser(row.userId).then((response) => {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
        getUserList();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
}
</script>

<style lang="less" scoped>
</style>